﻿using System;
using System.Xml;
using TextEditor.TextDocument.Document;
using System.Drawing;

namespace TextEditor
{


    public class ZYDocumentInfo
    {
        public ZYAttributeCollection Attributes = new ZYAttributeCollection();
        public bool AutoLogicDelete = true;
        public bool DesignMode = false;
        public bool EnableDataSource = false;
        public bool EnableScript = true;
        public bool FieldUnderLine = true;
        private int lineSpacing = 10;

        public bool LockForMark = true;
        public bool LogicDelete = false;
        public ZYTextDocument OwnerDocument = null;
        private int paragraphSpacing = 10;
        public bool Printing = false;
        public int SaveMode = 0;
        public bool SavePreViewText = false;
        public bool ShowAll = false;
        public bool ShowExpendHandle = true;
        public bool ShowMark = false;
        public bool ShowPageLine = false;
        public bool ShowParagraphFlag = true;
        public bool ShowScriptCompileError = true;
        public bool ShowScriptError = true;
        public int TableSapcing = 2;
        public int VisibleUserLevel = -1;
        public bool WordWrap = true;
        private string defaultFontName;
        private string defaultEnglishFontName;
        public int defaultFontSize = 12;

        public bool CopyTo(ZYDocumentInfo myInfo)
        {
            if (myInfo != null)
            {
                myInfo.OwnerDocument = this.OwnerDocument;
                this.Attributes.CopyTo(myInfo.Attributes);
                myInfo.FieldUnderLine = this.FieldUnderLine;
                myInfo.ShowMark = this.ShowMark;
                myInfo.AutoLogicDelete = this.AutoLogicDelete;
                myInfo.EnableDataSource = this.EnableDataSource;
                myInfo.SaveMode = this.SaveMode;
                myInfo.ShowPageLine = this.ShowPageLine;
                myInfo.ShowParagraphFlag = this.ShowParagraphFlag;
                myInfo.SavePreViewText = this.SavePreViewText;
                myInfo.VisibleUserLevel = this.VisibleUserLevel;
                myInfo.DesignMode = this.DesignMode;
                myInfo.LineSpacing = this.LineSpacing;
                myInfo.ParagraphSpacing = this.ParagraphSpacing;
                myInfo.WordWrap = this.WordWrap;
                myInfo.Printing = this.Printing;
                myInfo.LogicDelete = this.LogicDelete;
                myInfo.ShowAll = this.ShowAll;
                myInfo.ShowExpendHandle = this.ShowExpendHandle;
                myInfo.EnableScript = this.EnableScript;
                myInfo.ShowScriptError = this.ShowScriptError;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 行间距
        /// </summary>
        public int LineSpacing
        {
            get { return lineSpacing; }
            set
            {
                lineSpacing = value;
                this.Attributes.SetValue("lineSpacing", value);
            }
        }

        public string DefaultFontName
        {
            get
            {
                return this.defaultFontName;
            }
            set
            {
                this.defaultFontName = value;
                this.Attributes.SetValue("defaultFontName", value);
                this.OwnerDocument.View.DefaultFont = new Font(this.defaultFontName, this.defaultFontSize);
            }
        }

        public string DefaultEnglishFontName
        {
            get
            {
                return this.defaultEnglishFontName;
            }
            set
            {
                this.defaultEnglishFontName = value;
                this.Attributes.SetValue("defaultEnglishFontName", value);
                this.OwnerDocument.View.DefaultEnglishNumFont = new Font(this.defaultEnglishFontName, this.defaultFontSize);
            }
        }

        public int DefaulFontSize
        {
            get
            {
                return this.defaultFontSize;
            }
            set
            {
                this.defaultFontSize = value;
                this.Attributes.SetValue("defaultFontSize", value);
            }
        }


        /// <summary>
        /// 行间距
        /// </summary>
        public int ParagraphSpacing
        {
            get
            {
                return paragraphSpacing;
            }
            set
            {
                paragraphSpacing = value;
                this.Attributes.SetValue("paragraphSpacing", value);
            }
        }

        public bool FromXML(XmlElement myElement)
        {
            if (myElement == null)
            {
                this.Attributes.Clear();
                this.Version = "1.0";
            }
            else
            {
                this.Attributes.FromXML(myElement);
                this.LineSpacing = this.Attributes.GetInt32("lineSpacing");
                if (this.LineSpacing == 0)
                {
                    this.LineSpacing = 10;
                }
                this.ParagraphSpacing = this.Attributes.GetInt32("paragraphSpacing");
                if (this.ParagraphSpacing == 0)
                {
                    this.ParagraphSpacing = 10;
                }
                if (this.Attributes["defaultFontSize"] != null)
                {
                    this.DefaulFontSize = Convert.ToInt32(this.Attributes["defaultFontSize"].Value);
                }
                else
                {
                    this.DefaulFontSize = 12;
                }

                if (this.Attributes["defaultEnglishFontName"] != null)
                {
                    this.DefaultEnglishFontName = this.Attributes["defaultEnglishFontName"].Value.ToString();
                }
                else
                {
                    this.DefaultEnglishFontName = "Times New Roman";
                }


                if (this.Attributes["defaultFontName"] != null)
                {
                    this.DefaultFontName = this.Attributes["defaultFontName"].Value.ToString();
                }
                else
                {
                    this.DefaultFontName = "宋体";
                }
            }
            return true;
        }

        internal static string GetXMLName()
        {
            return "docsetting";
        }

        public bool ToXML(XmlElement myElement)
        {
            if (myElement != null)
            {
                this.Attributes.ToXML(myElement);
                return true;
            }
            return false;
        }

        public string CreateTime
        {
            get
            {
                return this.Attributes.GetString("createtime");
            }
            set
            {
                this.Attributes.SetValue("createtime", value);
            }
        }

        public string Creator
        {
            get
            {
                return this.Attributes.GetString("creator");
            }
            set
            {
                this.Attributes.SetValue("creator", value);
            }
        }

        public string FileName
        {
            get
            {
                return this.Attributes.GetString("filename");
            }
            set
            {
                this.Attributes.SetValue("filename", value);
            }
        }

        public string ID
        {
            get
            {
                return this.Attributes.GetString("id");
            }
            set
            {
                this.Attributes.SetValue("id", value);
            }
        }

        public int MarkCount
        {
            get
            {
                return this.Attributes.GetInt32("markcount");
            }
            set
            {
                this.Attributes.SetValue("markcount", value);
            }
        }

        public string Modifier
        {
            get
            {
                return this.Attributes.GetString("modifier");
            }
            set
            {
                this.Attributes.SetValue("modifier", value);
            }
        }

        public string ModifyTime
        {
            get
            {
                return this.Attributes.GetString("modifytime");
            }
            set
            {
                this.Attributes.SetValue("modifytime", value);
            }
        }

        public string Title
        {
            get
            {
                return this.Attributes.GetString("title");
            }
            set
            {
                this.Attributes.SetValue("title", value);
            }
        }

        public string Version
        {
            get
            {
                return this.Attributes.GetString("version");
            }
            set
            {
                this.Attributes.SetValue("version", value);
            }
        }
    }
}

